package com.fhz.day008;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button save;
    private Button getValue;
    private static final String TAG = "feng";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        save = (Button) findViewById(R.id.save);
        getValue = (Button) findViewById(R.id.getValue);


        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //存
                SharedPreferences sp = getSharedPreferences("login", MODE_PRIVATE);
                //开大编辑器
                SharedPreferences.Editor edit = sp.edit();

                edit.putBoolean("isLogin",true);
                edit.putString("name","小明");
                edit.putFloat("moeny",10f);
                edit.putInt("age",16);
                edit.putLong("lang",10);

//                edit.commit();
                edit.apply();
            }
        });
        
        getValue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences sp = getSharedPreferences("login", MODE_PRIVATE);
                int age = sp.getInt("age", 0);//16
                int age1 = sp.getInt("age1", 20);//20

                Log.i(TAG, "onClick: "+age);
                String string = sp.getString("name", "haha");
                Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show();

            }
        });

    }
}